package com.xiyuan.smartutils.template.engine.expression.operator.logic;

import com.xiyuan.smartutils.template.engine.VariableMap;
import com.xiyuan.smartutils.template.engine.exception.ExpressionException;
import com.xiyuan.smartutils.template.engine.expression.Expression;
import com.xiyuan.smartutils.template.engine.expression.operator.Operator;

/**
 * 取反运算(!)
 * 
 * @version v1.0.0 @author lgz 2020年12月6日 新建与整理
 */
public class _Not extends Operator
{
    private static final long serialVersionUID = 1L;
    private Expression next;
    
    public _Not(String path, int line, int column, Expression next)
    {
        super(path, line, column);
        this.next = next;
        this.assertSymbo("取反", next);
    }
    
    @Override
    public int getType()
    {
        return NOT;
    }
    
    @Override
    protected Object build(VariableMap variableMap)
    {
        Object value = next.execute(variableMap);
        if (value instanceof Boolean)
        {
            boolean v = (Boolean) value;
            return !v;
        }
        
        throw new ExpressionException(this, "取反表达式{" + this + "}格式不正常，要求结果不是布尔型");
    }
    
    @Override
    public String toString()
    {
        return new StringBuilder("!").append(next).toString();
    }
}
